//
//  WkwebViewConfig.swift
//  JJToolSwift
//
//  Created by heihei on 2024/5/19.
//  webView视图配置模型

import Foundation
import WebKit

/// webView视图配置模型
public struct WkwebViewConfig {
    
    public init() {
        
    }

    /// 初始化方法
    /// - Parameters:
    ///   - scriptMessageHandlerArray: WKScriptMessageHandler
    ///   - minFontSize: 默认最小字体字体
    ///   - isScrollEnabled: 拦截滑动 默认不拦截
    ///   - isBackgroundClear: 是否透明背景
    ///   - isShowScrollIndicator: 显示滚动条
    ///   - isAllowsBackForwardGestures: 显示滚动条
    ///   - isjavaScriptEnabled: 开启手势交互
    ///   - isAutomaticallyJavaScript: 是否允许加载javaScript
    ///   - isProgressHidden:是否影藏进度条
    ///   - progressHeight: 进度条高度
    ///   - progressTrackTintColor: 默认颜色
    ///   - progressTintColor: 加载颜色
    init(scriptMessageHandlerArray: [String], minFontSize: CGFloat, isScrollEnabled: Bool, isBackgroundClear: Bool, isShowScrollIndicator: Bool, isAllowsBackForwardGestures: Bool, isjavaScriptEnabled: Bool, isAutomaticallyJavaScript: Bool, isProgressHidden: Bool, progressHeight: CGFloat, progressTrackTintColor: UIColor, progressTintColor: UIColor) {
        self.scriptMessageHandlerArray = scriptMessageHandlerArray
        self.minFontSize = minFontSize
        self.isScrollEnabled = isScrollEnabled
        self.isBackgroundClear = isBackgroundClear
        self.isShowScrollIndicator = isShowScrollIndicator
        self.isAllowsBackForwardGestures = isAllowsBackForwardGestures
        self.isjavaScriptEnabled = isjavaScriptEnabled
        self.isAutomaticallyJavaScript = isAutomaticallyJavaScript
        self.isProgressHidden = isProgressHidden
        self.progressHeight = progressHeight
        self.progressTrackTintColor = progressTrackTintColor
        self.progressTintColor = progressTintColor
    }

    
    /// WKScriptMessageHandler
    /// 添加一个名称，就可以在JS通过这个名称发送消息：valueName自定义名字
    /// window.webkit.messageHandlers.valueName.postMessage({body: 'xxx'})
    public var scriptMessageHandlerArray: [String] = [String]()
    
    /// 默认最小字体字体
    public var minFontSize: CGFloat = 0
    
    /// 拦截滑动 默认不拦截
    public var isScrollEnabled:Bool = false
        
    /// 是否透明背景
    public var isBackgroundClear:Bool = false

    /// 显示滚动条
    public var isShowScrollIndicator: Bool = true
    
    /// 开启手势交互
    public var isAllowsBackForwardGestures: Bool = true
    
    /// 是否允许加载javaScript
    public var isjavaScriptEnabled: Bool = true
    
    /// 是否允许JS自动打开窗口的，必须通过用户交互才能打开
    public var isAutomaticallyJavaScript: Bool = true
    
    /// 是否影藏进度条
    public var isProgressHidden:Bool = false
    
    /// 进度条高度
    public var progressHeight:CGFloat = 3
    
    /// 默认颜色
    public var progressTrackTintColor:UIColor = UIColor.clear
    
    /// 加载颜色
    public var progressTintColor:UIColor = .red

}

//网页加载类型
public enum WkwebLoadType{
    
    /// 加载普通URL
    case URLString(url:String)
    
    /// 加载本地HTML(传名字就可以了)
    case HTMLName(name:String)
    
    /// 加载Html富文本
    case HTMLContent(content:String)
    
    /// 加载POST请求(url:请求URL，parameters：请求参数)
    case POST(url:String,parameters: [String:Any])
}

public protocol WKWebViewDelegate:Any {
    
    /// 服务器开始请求的时候调用
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
    
    /// 页面开始加载
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
    
    /// 页面加载完成
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
    
    /// 跳转失败的时候调用
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
    
    /// 内容加载失败
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)
    
    /// 执行JS注入方法
    func webViewUserContentController(_ scriptMessageHandlerArray:[String], didReceive message: WKScriptMessage)
    
    /// JS执行回调方法
    func webViewEvaluateJavaScript(_ result:Any?,error:Error?)
    
    /// 回调当前内容高度
    func webViewDocumentHeight(documentHeight:Double)
}

extension WKWebViewDelegate {
    /// 服务器开始请求的时候调用
    public func webView(_ webView: WKWebView,
                        decidePolicyFor navigationAction: WKNavigationAction,
                        decisionHandler: @escaping (WKNavigationActionPolicy) -> Void){}
    
    /// 页面开始加载
    public func webView(_ webView: WKWebView,
                        didStartProvisionalNavigation navigation: WKNavigation!){}
    
    /// 页面加载完成
    public  func webView(_ webView: WKWebView,
                         didFinish navigation: WKNavigation!){}
    
    /// 跳转失败的时候调用
    public func webView(_ webView: WKWebView,
                        didFail navigation: WKNavigation!,
                        withError error: Error){}
    
    /// 内容加载失败
    public func webView(_ webView: WKWebView,
                        didFailProvisionalNavigation navigation: WKNavigation!,
                        withError error: Error){}
    
    /// 执行JS注入方法
    public func webViewUserContentController(_ scriptMessageHandlerArray:[String],
                                             didReceive message: WKScriptMessage){}
    
    /// JS执行回调方法
    public func webViewEvaluateJavaScript(_ result:Any?,
                                          error:Error?){}
    
    /// 回调当前内容高度
    public func webViewDocumentHeight(documentHeight:Double){}
    

}
